﻿using Microsoft.EntityFrameworkCore;
using MySqlX.XDevAPI.Common;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZH.Infrastructure.Attr;
using ZH.Infrastructure.Dto.Request.Emp;
using ZH.Infrastructure.Dto.Response;
using ZH.Infrastructure.Dto.Response.admin;
using ZH.Infrastructure.Dto.Response.Emp;
using ZH.Infrastructure.EFCore.MySql;
using ZH.Infrastructure.IOC;
using ZH.Infrastructure.Tools;
using ZH.Service.Interface;

namespace ZH.Service.Instance
{

    [Inject_]
    public class Emp_Service :Base_Service,I_Emp_Service
    {
        private readonly Emp_IOC _emp_IOC;
        private readonly Dictionary_IOC _dictionary_IOC;

        public Emp_Service(Emp_IOC emp_IOC,Dictionary_IOC dictionary_IOC)
        {
            _emp_IOC=emp_IOC;
            _dictionary_IOC = dictionary_IOC;
        }

        public async Task<Api_Response_Dto> Add_Emp(Emp_Request_Dto dto)
        {
   
            Employee emp = new Employee();
            emp.EmpId = Config.GUID2();
            emp.Ename=dto.Dname;
            emp.Egender=dto.dgender;
            emp.EhireDate = dto.dhire_date;
            emp.Especialty=dto.dspecialty;
            emp.EepartmentId=dto.department_id;
            emp.Estatus = "在职";
            emp.EavatarUrl=dto.davatar_url;
            emp.EcreatedAt=DateTime.Now;
            emp.EupdatedAt=DateTime.Now;
            _emp_IOC._emp_EFCore.Add(emp);
            await _emp_IOC._emp_EFCore.SaveChangesAsync();


            return Result(1,"ok");



        }
        /// <summary>
        ///删除员工
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Api_Response_Dto> Delete_Emp(string? id)
        {
          var data= _emp_IOC._emp_EFCore.QueryAll(d=>d.EmpId==id).FirstOrDefault();
            if (data != null) {
                return Result(1, "没有该员工");
            }
            _emp_IOC._emp_EFCore.Delete(data);
           var code=await _emp_IOC._emp_EFCore.SaveChangesAsync();
            return Result(code, "删除成功");
        }

        public async Task<Api_Response_Dto> Get_Cashier_Emp()
        {
            var dict = _dictionary_IOC._dictionary_EFCore.QueryAll();

            var data = await _emp_IOC._emp_EFCore.QueryAll().ToListAsync();
            var total = data.Count();
            return Result(1, "ok", new { total,data });
        }

        /// <summary>
        /// 获取员工列表
        /// </summary>
        /// <param name="emp_Response_Dto"></param>
        /// <returns></returns>
        public async Task<Api_Response_Dto> Get_Emp(Emp_Response_Dto dto)
        {

            var data = await _emp_IOC._emp_EFCore.QueryAll(out int total,
                  Convert.ToInt32(dto.page), Convert.ToInt32(dto.limit),
                  true,
                  o => o.EmpId,
                    d => (string.IsNullOrEmpty(dto.key) || d.Ename.Contains(dto.key))
                  ).ToListAsync();
            return Result(1, "ok", new
            {
                total,
                data
            });
        }

        public async Task<Api_Response_Dto> Update_Emp(Emp_Request_Dto dto)
        {
           Employee emp=new Employee();
            emp.EmpId = dto.DId;
            emp.Ename=dto.Dname;
            emp.Egender=dto.dgender;
            emp.Ephone=dto.dphone;
            emp.Especialty=dto.dspecialty;
            emp.EepartmentId = dto.department_id;
            emp.Estatus=dto.dstatus;
            emp.EupdatedAt=DateTime.Now;
            _emp_IOC._emp_EFCore.Update(emp);
            var code=await _emp_IOC._emp_EFCore.SaveChangesAsync();
            return Result(code,"修改成功");
        }
    }
}

